#include "threadPool.h"
void *threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    dirStack_t dirStack;
    initDirStack(&dirStack);
    while(1){
        int netfd;
        pthread_mutex_lock(&pthreadPool->mutex);

        while(pthreadPool->taskQueue.queueSize == 0 && pthreadPool->exitFlag == 0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        if(pthreadPool->exitFlag == 1){
            printf("A worker is going to exit!\n");
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }
        netfd = pthreadPool->taskQueue.pFront->netfd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        printf("i am niger! i got netfd = %d\n",netfd);
        while(1){
            train_t train;

            char sendBuf[4096]={0};
            char absBuf[4096] = {0};
            int op,ret,rd;
            ret = recv(netfd,&train,sizeof(train),MSG_WAITALL); // 接收传递过来的cmd，data
            if(ret == 0){
                close(netfd);
                break;
            }else if(ret < 0){
                // 记得要加上返回连接问题信息
                close(netfd);
                break;
            }
            int goal = train.cmd; // 将操作数取出赋值给goal
            char *bufRead = train.data; // 将客户端传进来的字符串取出
            switch(goal){
          //  case 0:
          //      // cd,

          //      bzero(absBuf,sizeof(absBuf));
          //      getAbsDir(&dirStack,bufRead,absBuf);
          //      rd = access(absBuf,F_OK);
          //      if(rd == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:wrong path");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //          continue;
          //      }
          //      ret = cdFunc(&dirStack,bufRead);
          //      LOG_CHECK(ret,"cdFunc");
          //      if(ret == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:cd");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      }
          //      break;
                case 0:
                // cd,

                bzero(absBuf,sizeof(absBuf));
                getAbsDir(&dirStack,bufRead,absBuf);
                rd = access(absBuf,F_OK);
                if(rd == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:wrong path");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                    continue;
                }
                ret = cdFunc(&dirStack,bufRead);
                LOG_CHECK(ret,"cdFunc");
                if(ret == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:cd");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                break;
            case 1:
                // ls
                /* rd = access(bufRead,F_OK); */
                /* if(rd == -1){ */
                /*     bzero(sendBuf,sizeof(sendBuf)); */
                /*     strcpy(sendBuf,"[ERROR]:wrong path"); */
                /*     send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL); */
                /*     break; */
                /* } */
                ret = lsFunc(&dirStack, bufRead,sendBuf);
                LOG_CHECK(ret,"lsFunc");
                if(ret == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:ls");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                break;
            case 2:
                // pwd
                bzero(sendBuf,sizeof(sendBuf));
                ret = pwdFunc(&dirStack,sendBuf);
                LOG_CHECK(ret,"pwdFunc");
                if(ret == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:pwd");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                sleep(1);
                break;
            case 3:
                //mkdir
                ret = mkdirFunc(&dirStack,bufRead);
                LOG_CHECK(ret,"mkdirFunc");
                if(ret == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:mkdir");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                break;
            case 4:
                // rmdir
                bzero(absBuf,sizeof(absBuf));
                getAbsDir(&dirStack,bufRead,absBuf);
                rd = access(absBuf,F_OK);
                if(rd == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:wrong path");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                    continue;
                }
                ret = rmdirFunc(&dirStack,bufRead,sendBuf);
                LOG_CHECK(ret,"rmdirFunc");
                if(ret == -1){
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                break;
            case 5:
                // puts 传输
                bzero(absBuf,sizeof(absBuf));
                getAbsDir(&dirStack,bufRead,absBuf);
                /* rd = access(absBuf,F_OK); */
                /* if(rd == -1){ */
                /*     bzero(sendBuf,sizeof(sendBuf)); */
                /*     strcpy(sendBuf,"[ERROR]:wrong path"); */
                /*     send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL); */
                /*     continue; */
                /* } */
                send(netfd,"start recvFile",14,MSG_NOSIGNAL);
                op = putsFile(netfd,absBuf);
                if(op == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:getsFile");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                LOG_CHECK(op,"getsFile");
                break;

            case 6:
                // getts 上传
                bzero(absBuf,sizeof(absBuf));
                getAbsDir(&dirStack,bufRead,absBuf);
                rd = access(absBuf,F_OK);
                if(rd == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:wrong path");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                    continue;
                }
                send(netfd,"start sendFile",14,MSG_NOSIGNAL);
                op = getsFile(netfd,absBuf);
                if(op == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:putsFile");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                LOG_CHECK(op,"putsFile");
                break;
            case 7:
                // remove
                 
                bzero(absBuf,sizeof(absBuf));
                getAbsDir(&dirStack,bufRead,absBuf);
                rd = access(absBuf,F_OK);
                if(rd == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:wrong path");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                    continue;
                }
          //  case 1:
          //      // ls
          //      /* rd = access(bufRead,F_OK); */
          //      /* if(rd == -1){ */
          //      /*     bzero(sendBuf,sizeof(sendBuf)); */
          //      /*     strcpy(sendBuf,"[ERROR]:wrong path"); */
          //      /*     send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL); */
          //      /*     break; */
          //      /* } */
          //      ret = lsFunc(&dirStack, bufRead,sendBuf);
          //      LOG_CHECK(ret,"lsFunc");
          //      if(ret == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:ls");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      }
          //      send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      break;
          //  case 2:
          //      // pwd
          //      bzero(sendBuf,sizeof(sendBuf));
          //      ret = pwdFunc(&dirStack,sendBuf);
          //      LOG_CHECK(ret,"pwdFunc");
          //      if(ret == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:pwd");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      }
          //      send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      sleep(1);
          //      break;
          //  case 3:
          //      //mkdir
          //      ret = mkdirFunc(&dirStack,bufRead);
          //      LOG_CHECK(ret,"mkdirFunc");
          //      if(ret == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:mkdir");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      }
          //      break;
          //  case 4:
          //      // rmdir
          //      bzero(absBuf,sizeof(absBuf));
          //      getAbsDir(&dirStack,bufRead,absBuf);
          //      rd = access(absBuf,F_OK);
          //      if(rd == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:wrong path");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //          continue;
          //      }
          //      ret = rmdirFunc(&dirStack,bufRead,sendBuf);
          //      LOG_CHECK(ret,"rmdirFunc");
          //      if(ret == -1){
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      }
          //      break;
          //  case 5:
          //      // gets 传输
          //      bzero(absBuf,sizeof(absBuf));
          //      getAbsDir(&dirStack,bufRead,absBuf);
          //      rd = access(absBuf,F_OK);
          //      if(rd == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:wrong path");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //          continue;
          //      }
          //      op = getsFile(netfd,absBuf);
          //      if(op == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:getsFile");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      }
          //      LOG_CHECK(op,"getsFile");
          //      break;

          //  case 6:
          //      // puts 接收
          //      bzero(absBuf,sizeof(absBuf));
          //      getAbsDir(&dirStack,bufRead,absBuf);
          //      rd = access(absBuf,F_OK);
          //      if(rd == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:wrong path");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //          continue;
          //      }
          //      op = putsFile(netfd,absBuf);
          //      if(op == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:putsFile");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //      }
          //      LOG_CHECK(op,"putsFile");
          //      break;
          //  case 7:
          //      // remove

          //      bzero(absBuf,sizeof(absBuf));
          //      getAbsDir(&dirStack,bufRead,absBuf);
          //      rd = access(absBuf,F_OK);
          //      if(rd == -1){
          //          bzero(sendBuf,sizeof(sendBuf));
          //          strcpy(sendBuf,"[ERROR]:wrong path");
          //          send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
          //          continue;
          //      }
                op = removeFile(netfd,absBuf);
                if(op == -1){
                    bzero(sendBuf,sizeof(sendBuf));
                    strcpy(sendBuf,"[ERROR]:removeFile");
                    send(netfd,sendBuf,strlen(sendBuf),MSG_NOSIGNAL);
                }
                LOG_CHECK(op,"removeFile");
                break;
            }
        }
    }
}

int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum;i++){
        int ret = pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
        ERROR_CHECK(ret,-1,"pthread_create");
        printf("create pthread ok\n");
    }
    return 0;
}
